// #include <WiFi.h>
// #include <NTPClient.h>
// #include <WiFiUdp.h>
// #include <Adafruit_NeoPixel.h>

// // WiFi credentials
// const char* ssid     = "Redmi K70";
// const char* password = "12345679";

// // NTP Client settings
// WiFiUDP ntpUDP;
// NTPClient timeClient(ntpUDP, "pool.ntp.org", 8 * 3600, 60000); // Offset for UTC+8

// // LED settings
// #define PIN        6
// #define NUMPIXELS  256
// Adafruit_NeoPixel strip(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

// // Digit bitmaps for 8x8 matrix
// const uint64_t digitBitmaps[10] = {
//   0x3C6666663C000000, // 0
//   0x1818181C18000000, // 1
//   0x3C060C303E000000, // 2
//   0x3C060C063C000000, // 3
//   0x66663E0606000000, // 4
//   0x3E301C063C000000, // 5
//   0x3C303C663C000000, // 6
//   0x3E060C0C0C000000, // 7
//   0x3C663C663C000000, // 8
//   0x3C663E063C000000  // 9
// };

// void setup() {
//   // Initialize serial communication
//   Serial.begin(115200);
  
//   // Initialize LED strip
//   strip.begin();
//   strip.setBrightness(20); // Set brightness to 20
//   strip.show(); // Initialize all pixels to 'off'
  
//   // Connect to WiFi
//   WiFi.begin(ssid, password);
//   while (WiFi.status() != WL_CONNECTED) {
//     delay(1000);
//     Serial.println("Connecting to WiFi...");
//   }
//   Serial.println("Connected to WiFi");

//   // Initialize NTP Client
//   timeClient.begin();
// }

// void loop() {
//   // Update the time
//   timeClient.update();
  
//   // Get hours and minutes
//   int hours = timeClient.getHours();
//   int minutes = timeClient.getMinutes();
  
//   // Extract digits
//   int hourTens = hours / 10;
//   int hourUnits = hours % 10;
//   int minuteTens = minutes / 10;
//   int minuteUnits = minutes % 10;

//   // Display digits on the 4 8x8 matrices
//   displayDigit(0, digitBitmaps[hourTens]);
//   displayDigit(1, digitBitmaps[hourUnits]);
//   displayDigit(2, digitBitmaps[minuteTens]);
//   displayDigit(3, digitBitmaps[minuteUnits]);

//   delay(1000); // Update every second
// }

// void displayDigit(int matrixIndex, uint64_t bitmap) {
//   for (int y = 0; y < 8; y++) {
//     for (int x = 0; x < 8; x++) {
//       int pixelIndex = matrixIndex * 64 + y * 8 + x;
//       if (bitmap & ((uint64_t)1 << (y * 8 + x))) {
//         strip.setPixelColor(pixelIndex, strip.Color(255, 255, 255)); // White
//       } else {
//         strip.setPixelColor(pixelIndex, strip.Color(0, 0, 0)); // Off
//       }
//     }
//   }
//   strip.show();
// }
#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <Adafruit_NeoPixel.h>

// WiFi credentials
const char* ssid     = "Redmi K70";
const char* password = "12345679";

// NTP Client settings
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", 8 * 3600, 60000); // Offset for UTC+8

// LED settings
#define PIN        6
#define NUMPIXELS  256
Adafruit_NeoPixel strip(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

// Digit bitmaps for 8x8 matrix
const uint64_t digitBitmaps[10] = {
  0x3C6666666666663C, // 0
  0x7E181818181C1810, // 1
  0x7E02021C2040423C, // 2
  0x3C4240403840423C, // 3
  0x101010FF11121418, // 4
  0x3C4240403C02023C, // 5
  0x3C4242423E040830, // 6
  0x101010103020407C, // 7
  0x3C42423C3C42423C, // 8
  0x3C42407C4242423C  // 9
};


// Rainbow color variables
uint16_t rainbowHue = 0; // Starting hue for rainbow effect

void setup() {
  // Initialize serial communication
  Serial.begin(115200);
  
  // Initialize LED strip
  strip.begin();
  strip.setBrightness(20); // Set brightness to 20
  strip.show(); // Initialize all pixels to 'off'
  
  // Connect to WiFi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");

  // Initialize NTP Client
  timeClient.begin();
}

void loop() {
  // Update the time
  timeClient.update();
  
  // Get hours and minutes
  int hours = timeClient.getHours();
  int minutes = timeClient.getMinutes();
  
  // Extract digits
  int hourTens = hours / 10;
  int hourUnits = hours % 10;
  int minuteTens = minutes / 10;
  int minuteUnits = minutes % 10;

  // Display digits on the 4 8x8 matrices with rainbow effect
  displayDigit(0, digitBitmaps[hourTens]);
  displayDigit(1, digitBitmaps[hourUnits]);
  displayDigit(2, digitBitmaps[minuteTens]);
  displayDigit(3, digitBitmaps[minuteUnits]);

  // Increment rainbow hue for next frame
  rainbowHue += 1; // Change this value to adjust speed of rainbow effect

  delay(100); // Update every 100 milliseconds (adjust as needed)
}

void displayDigit(int matrixIndex, uint64_t bitmap) {
  for (int y = 0; y < 8; y++) {
    for (int x = 0; x < 8; x++) {
      int pixelIndex = matrixIndex * 64 + y * 8 + x;
      if (bitmap & ((uint64_t)1 << (y * 8 + x))) {
        uint8_t hue = rainbowHue + (x * 8) + (y * 8); // Calculate hue based on position
        strip.setPixelColor(pixelIndex, Wheel(hue)); // Set pixel color using rainbow wheel function
      } else {
        strip.setPixelColor(pixelIndex, strip.Color(0, 0, 0)); // Off
      }
    }
  }
  strip.show();
}

// Rainbow color wheel function
uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if (WheelPos < 85) {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if (WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}


